/*
 * Copyright 2007 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.dev.jas.as.impl;

import com.google.gwt.dev.js.ast.JsBinaryOperation;
import com.google.gwt.dev.js.ast.JsBinaryOperator;
import com.google.gwt.dev.js.ast.JsBlock;
import com.google.gwt.dev.js.ast.JsContext;
import com.google.gwt.dev.js.ast.JsExprStmt;
import com.google.gwt.dev.js.ast.JsExpression;
import com.google.gwt.dev.js.ast.JsFunction;
import com.google.gwt.dev.js.ast.JsInvocation;
import com.google.gwt.dev.js.ast.JsModVisitor;
import com.google.gwt.dev.js.ast.JsName;
import com.google.gwt.dev.js.ast.JsNameRef;
import com.google.gwt.dev.js.ast.JsNullLiteral;
import com.google.gwt.dev.js.ast.JsProgram;
import com.google.gwt.dev.js.ast.JsReturn;
import com.google.gwt.dev.js.ast.JsScope;
import com.google.gwt.dev.js.ast.JsStatement;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Transforms null.x -> $Null.x to make actionscript compiler happy.
 */
public final class JsNullDeferenceRemover {

  static class NullDerefVisitor extends JsModVisitor {

    @Override
    public void endVisit(JsNameRef x, JsContext ctx) {
      JsExpression qualifier = x.getQualifier();
      if (qualifier == JsNullLiteral.INSTANCE) {
        x.setQualifier(new JsNameRef(x.getSourceInfo(), "$Null"));
      }
    }
  }

  public static void exec(JsProgram program) {
    new NullDerefVisitor().accept(program);
  }

  private JsNullDeferenceRemover() {
  }
}
